package com.hxzy.tms.action;

import java.io.File;
import java.util.List;

import com.hxzy.tms.action.vo.TaskVo;
import com.hxzy.tms.entity.Project;
import com.hxzy.tms.entity.Task;
import com.hxzy.tms.entity.Team;
import com.hxzy.tms.entity.User;
import com.hxzy.tms.entity.UserTask;
import com.hxzy.tms.service.AttachmentService;
import com.hxzy.tms.service.ProjectService;
import com.hxzy.tms.service.ServiceResult;
import com.hxzy.tms.service.TaskService;
import com.hxzy.tms.session.SessionHolder;

public class TaskAction extends AbstractAction {

	private static final long serialVersionUID = -7824115818037068463L;
	private ProjectService projectService;
	private AttachmentService attachmentService;
	private List<Project> project;
	private TaskService taskService;
	private TaskVo task;
	private Team team;
	private File file;
	private String fileFileName;
	private String savePath;
	private String fileContentType;

	private Project p;
	
	public String form() throws Exception {
		this.project=projectService.projectList(team.getId()).data;
		return INPUT;
	}
	
	public String task() throws Exception {
		
		if(isGET()&&task.getId()!=null){
		/*	Task t=taskService.read(task.getId()).data;
			this.task.setProject(t.getProject());
			this.task.setDesc(t.getDesc());
			this.task.setTitle(t.getTitle());
			this.task.setEndTime(new Date(t.getGmtFinished()*1000));
			this.task.setStartTime(new Date(t.getGmtStart()*1000));*/
			return INPUT;
		}
	
		ServiceResult<Task> result=null;
		if(task.getId()!=null){
			result=taskService.update(task.getId(), task.getProject().getId(), task.getTeam().getId(), task.getTitle(), task.getDesc(),file.getAbsolutePath(), task.getStartTime(), task.getEndTime());
		}else{
			File srcFile=file;
			File destFile=new File(savePath+File.separator+fileFileName);
			//项目id,团队id
			result=taskService.create(task.getProject().getId(), team.getId(), task.getTitle(), task.getDesc(),destFile.getPath(), task.getStartTime(),task.getEndTime());
			attachmentService.upload(srcFile, fileContentType, destFile, SessionHolder.get().getId(), SessionHolder.get().getId());
		}
		
		if(result.status!=1){
			switch (result.status) {
			case -1:
				addFieldError("task.title", result.message);
				break;
			case -2:
				addFieldError("task.desc", result.message);
				break;
			case -3:
				addActionError(result.message);
				break;
			case -4:
				addActionError(result.message);
			default:
				break;
			}
			return INPUT;
		}
		return SUCCESS;
	}

	public String commitTask(){
		User user=new User();
		user.setId(11L);
		SessionHolder.set(user);
		if(isGET()){
			return INPUT;
		}
		ServiceResult<UserTask>result=null;
		File srcFile=file; 
		File destFile=new File(savePath+File.separator+fileFileName);
		result=taskService.update(52L, destFile.getPath());
		attachmentService.upload(srcFile, fileContentType, destFile, SessionHolder.get().getId(), SessionHolder.get().getId());
		if(result.status==1){
		return SUCCESS;
		}else{
			addActionError(result.message);
			return INPUT;
		}
	}
/*	public String projectList() throws IOException{
		User user=new User();
		user.setId(11L);
		SessionHolder.set(user);
		Gson gson=new Gson();
		this.project=projectService.projectList(1L).data;
		String teamJson=gson.toJson(project);
		HttpServletResponse response=ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		response.setHeader("Content-Type", "application/json");
		response.getWriter().write(teamJson);
		return null;
	}*/
	public TaskVo getTask() {
		return task;
	}


	public void setTask(TaskVo task) {
		this.task = task;
	}


	public List<Project> getProject() {
		return project;
	}


	public String getFileFileName() {
		return fileFileName;
	}


	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}


	public AttachmentService getAttachmentService() {
		return attachmentService;
	}


	public void setAttachmentService(AttachmentService attachmentService) {
		this.attachmentService = attachmentService;
	}


	public void setProject(List<Project> project) {
		this.project = project;
	}


	public ProjectService getProjectService() {
		return projectService;
	}

	public void setProjectService(ProjectService projectService) {
		this.projectService = projectService;
	}

	public TaskService getTaskService() {
		return taskService;
	}

	public void setTaskService(TaskService taskService) {
		this.taskService = taskService;
	}

	public Team getTeam() {
		return team;
	}

	public void setTeam(Team team) {
		this.team = team;
	}


	public File getFile() {
		return file;
	}


	public void setFile(File file) {
		this.file = file;
	}


	public String getSavePath() {
		return savePath;
	}


	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}


	public String getFileContentType() {
		return fileContentType;
	}


	public void setFileContentType(String fileContentType) {
		this.fileContentType = fileContentType;
	}

	public Project getP() {
		return p;
	}

	public void setP(Project p) {
		this.p = p;
	}
	
	

}